package com.wjc.server.service;


import com.wjc.server.dto.CourseContentFileDto;
import com.wjc.server.entity.CourseContentFile;
import com.wjc.server.entity.CourseContentFileExample;
import com.wjc.server.mapper.CourseContentFileMapper;
import com.wjc.server.util.CopyUtil;
import com.wjc.server.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CourseContentFileService {
    @Resource
    private CourseContentFileMapper courseContentFileMapper;

    @Resource
    private SnowFlake snowFlake;

    //根据课程id查询
    public List<CourseContentFileDto> list(Long courseId){
        CourseContentFileExample courseContentFileExample = new CourseContentFileExample();
        courseContentFileExample.createCriteria().andCourseIdEqualTo(courseId);
        List<CourseContentFile> courseContentFileList= courseContentFileMapper.selectByExample(courseContentFileExample);
        List<CourseContentFileDto> courseContentFileDtosList = CopyUtil.copyList(courseContentFileList,CourseContentFileDto.class);
        return courseContentFileDtosList;

    }

    //保存
    public void save(CourseContentFileDto courseContentFileDto){

        CourseContentFile courseContentFile=CopyUtil.copy(courseContentFileDto,CourseContentFile.class);
        if (ObjectUtils.isEmpty(courseContentFile.getId())){
            //新增
            courseContentFile.setId(snowFlake.nextId());
            courseContentFileMapper.insert(courseContentFile);
        }else {
            courseContentFileMapper.updateByPrimaryKey(courseContentFile);
        }

    }

    public void delete(Long id){

        courseContentFileMapper.deleteByPrimaryKey(id);
    }
}
